const fs = require('fs')
const { v4: uuidv4 } = require('uuid');
import { rejects } from 'assert';
import { resolve } from 'path';
import { connection } from '../my_sql.js/index'


function mzz(router) {
    router.get('/abc', ctx => {
        ctx.body = 'comeDeWei'
    })
    // 注册 用户获取验证码，没有注册自动注册
    router.get('/getPhoneCode', async ctx => {
        let phone = ctx.query.phone
        // 如果用户存在，发验证码(6位随机数)
        let code = sms_util.randomCode(6)
       ctx.body = await new Promise((resolve, reject) => {
            connection.query(`select * from user where tel_phone = '${phone}'`, (err, res) => {
                if (err) console.log(err);
                if (res.length > 0) {
                    resolve('此用户已存在')
                    sms_util.sendCode(phone, code, (success) => {
                        if (success) {
                            resolve({
                                code: 200,
                                data: {
                                    code
                                }
                            })
                        }
                    })
                } else {
                    // resolve(false)
                    var sql_str = `insert into user(name,sex,address,sign,head,phoneNumber,birthday,attention,fans) values('${name}','${sex}','${address}','${sign}','${head}','${phoneNumber}','${birthday}','${attention}','${fans}')`
                    connection.query(sql_str,(err,res) =>{
                        if (err) {
                            reject(err);
                        }else{
                            resolve(res)
                        }
                       
                    })
                }
            })
        })
    
    

    })
    // 用户登录


    // 查询用户
    router.get('/UserInfo', async ctx => {
        ctx.body = await new Promise((resolve, reject) => {
            connection.query(`select * from user`, (err, res) => {
                if (err) resolve(err);
                resolve(res)
            })
        })
    })
}

export {
    mzz
}